#!/usr/bin/python

import sys
from Bio import SeqIO

sf = open(sys.argv[1], "rU")

record = SeqIO.read(sf, "fasta")

start = raw_input("Enter start: ")

stop = raw_input("Enter stop: ")

strand = raw_input("Enter strand (+ or -): ")

fixtype = raw_input("cut or extend?: ")

start_codons = {'ATG': 'Start', 'CTG': 'Start', 'GTG': 'Start', 'TTG': 'Start'}
stop_codons = {'TAA': 'Stop', 'TAG': 'Stop', 'TGA': 'Stop'}

n = 3

triplets = []

if fixtype == "extend":
    if strand == "+":
        subseq = record.seq[int(start):int(stop)]
        num = len(subseq)
        print "Substring = ", subseq
        print "Length = ", num
        while n <= num:
            t = subseq[num-n:num-n+3]
            triplets.append(t)
            n += 3
    else:
        subseq = record.seq[int(start):int(stop)].reverse_complement()
        num = len(subseq)
        print "Substring = ", subseq
        print "Length = ", num
        while n <= num:
            t = subseq[num-n:num-n+3]
            triplets.append(t)
            n += 3

    print "Triplets: ",
else:
    if strand == "+":
        subseq = record.seq[int(start):int(stop)]
        num = len(subseq)
        print "Substring = ", subseq
        print "Length = ", num
        while n <= num:
            t = subseq[0+n-3:0+n]
            triplets.append(t)
            n += 3
    else:
        subseq = record.seq[int(start):int(stop)].reverse_complement()
        num = len(subseq)
        print "Substring = ", subseq
        print "Length = ", num
        while n <= num:
            t = subseq[0+n-3:0+n]
            triplets.append(t)
            n += 3
    print "Triplets: ",


if fixtype == "extend":
    m = -1
    neg = -len(triplets)
    while m >= neg:
        print triplets[m],
        m -= 1
else:
    m = 0
    pos = len(triplets)
    while m < pos:
        print triplets[m],
        m += 1

